package com.hetao.xx.service;

import com.hetao.xx.Global;
import com.hetao.xx.game.FightUser;
import com.hetao.xx.game.model.User;
import com.hetao.xx.orm.NpcVo;
import com.hetao.xx.orm.UserVo;
import com.hetao.xx.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

public class FightManager {
    private final String TAG = getClass().getSimpleName();

    Global global;
    public FightManager(Global global) {
        this.global = global;
    }

    private final List<NpcVo> enemy = new ArrayList<>(4);
    private final List<String> message = new ArrayList<>(4);

    public void setEnemy(NpcVo npcVo){
        enemy.clear();
        enemy.add(npcVo);
    }
    public void setEnemy(List<NpcVo> npcVo){
        enemy.clear();
        enemy.addAll(npcVo);
    }

    public boolean fightNpc() {
        User user = global.getUser();

        NpcVo npcVo = enemy.get(0);
        NpcVo enemy = CommonUtil.CloneNpcVo(npcVo);
        UserVo mine = CommonUtil.CloneUserVo(user.info);

        FightUser fightUser = new FightUser(mine);
        FightUser npc = new FightUser(enemy);

        message.clear();
        calFightFrontEnhance(fightUser, npc);
        startFight(fightUser, npc);

        return !fightUser.dead;
    }

    public List<String> getMessage() {
        return message;
    }

    private void calFightFrontEnhance(FightUser fightUser, FightUser npc) {
        message.add(String.format("<%s>进攻<%s>, <%s>先手...", fightUser.user.name, npc.user.name, fightUser.user.name));
        message.add("战斗开始...");
    }

    /**
     * 战斗增幅计算  光环
     * @param user
     */
    private void calFrontEnhance(UserVo user) {

    }



    public void startFight(FightUser a, FightUser b) {

        b.timeLine = 100;
        while (true) {
            if (a.timeLine <= b.timeLine) {
                //a->b
                message.addAll(a.fight(b));
            } else {
                //b->a
                message.addAll(b.fight(a));
            }
            if (a.dead || b.dead) {
                return;
            }

        }
    }
}
